package com.fingard.dsp.bank.directbank.rccosd01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IBalance;
import com.fingard.dsp.bank.reqrespobj.balance.Req7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.Req7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7505HisBalQry;
import com.fingard.dsp.bank.reqrespobj.balance.RetAts7506BalQry;
import com.fingard.dsp.bank.reqrespobj.balance.detail.RetAtsBalDetail;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;

/**
 * @author Parker on 2019/7/17.
 * @version 1.0
 */
public class RCCOSD01Balance extends RCCOSD01Base implements IBalance {
    //单个余额查询和批量余额查询，统一采用流处理方式
    //1、循环调用Req7506BalQry.readRecord()读取一个请求明细，然后写入发银行的XML对象
    //2、发银行
    //3、循环调用XmlTextReader.read()读取一个银行返回节点，赋值返回ATS的明细对象，
    //   然后调用RetAts7506BalQry.writeRetDetail()，写入返回ATS的明细
    @Override
    public RetAts7506BalQry d_7506_BalQry(Req7506BalQry p_req7506) throws Exception {//余额查询
        RetAts7506BalQry retAts = new RetAts7506BalQry(p_req7506.reqHeader);
        retAts.writeStart();//写返回ATS的统一头部信息
        try {
            //赋值通用参数
            RCCOSD01Header bankHeader = new RCCOSD01Header();
            bankHeader.bsnCode = "QRCB001";
            while (p_req7506.readRecord()) {
                XmlTextWriter xmlWriter = new XmlTextWriter();
                RetAtsBalDetail detail = new RetAtsBalDetail();
                bankHeader.serialNo = idHelper.getNextIDStr();
                writeCommonHeader(xmlWriter,bankHeader);
                XElement tmpXElement = new XElement("Body", "");
                tmpXElement.addChild(new XElement("PayerAcNo", p_req7506.readedDetail.actNum));
                tmpXElement.addChild(new XElement("PayerCurrency", convertCurToBank(p_req7506.readedDetail.actCur)));
                xmlWriter.writeXElement(tmpXElement);
                xmlWriter.writeEndElement();
                xmlWriter.writeEndDocument();
                String[] retStrArr = sendToBank(xmlWriter.toXmlString(),bankHeader);
                /*String[] retStrArr = new String[]{"","<Message>\n" +
                        "<Head>\n" +
                        "<IBSReturnCode>000000</IBSReturnCode>\n" +
                        "<IBSReturnMsg>xxxxxx</IBSReturnMsg>\n" +
                        "<IBSJnlNo>42</IBSJnlNo>\n" +
                        "<serialNo>1</serialNo>\n" +
                        "</Head>\n" +
                        "<Body>\n" +
                        "<Map>\n" +
                        "<CurrentBalance>10000.00</CurrentBalance>\n" +
                        "<AvailBalance>10000.00</AvailBalance>\n" +
                        "</Map>\n" +
                        "</Body>\n" +
                        "</Message>"};*/
                if (retStrArr[0].length() == 0) {
                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(retStrArr[1], tmpSbMsg);
                    if(tmpBkReader != null){
                        String tmpRetCode = tmpBkReader.getFirstText("/Message/Head/IBSReturnCode");//返回码
                        String tmpRetInfo = tmpBkReader.getFirstText("/Message/Head/IBSReturnMsg");//返回信息
                        if(tmpRetCode != null && "000000".equals(tmpRetCode)){
                            detail = new RetAtsBalDetail();
                            detail.respCode = "0";
                            detail.respInfo = tmpRetInfo;
                            detail.availBal = tmpBkReader.getFirstText("/Message/Body/Map/AvailBalance");
                            detail.currBal = tmpBkReader.getFirstText("/Message/Body/Map/CurrentBalance");
                            detail.actNum = p_req7506.readedDetail.actNum;
                            detail.actCur = p_req7506.readedDetail.actCur;
                            detail.actArea = p_req7506.readedDetail.actArea;
                            retAts.writeRetDetail(detail, p_req7506.isBatch);
                        }else{
                            detail.respCode = "-999";
                            detail.respInfo = tmpRetInfo;
                            retAts.writeRetDetail(detail, p_req7506.isBatch);
                        }
                    } else {
                        detail.respCode = "-999";
                        detail.respInfo = "银行返回数据为空";
                        retAts.writeRetDetail(detail, p_req7506.isBatch);
                    }
                } else {
                    detail.respCode = "-999";
                    detail.respInfo = retStrArr[1];
                    retAts.writeRetDetail(detail, p_req7506.isBatch);
                }
            }
        }catch (Exception e) {
            retAts.writeRespCode("-999");
            retAts.writeRespInfo(e.getMessage());
            e.printStackTrace();
        }
        retAts.writeEnd();
        return retAts;
    }

    @Override
    public RetAts7505HisBalQry d_7505_BalQry(Req7505HisBalQry p_req7505) throws Exception {
        return null;
    }
}
